package cn.qqncn.furniture.controller;

import cn.qqncn.furniture.pojo.domain.Admin;
import cn.qqncn.furniture.service.AdminService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zkw
 * @ClassName AdminController.java
 * @Description TODO
 * @createTime 2020年12月30日 14:53:00
 */
@Controller
@RequestMapping("back/admin")
public class AdminController {
    @Resource
    AdminService adminService;

    /**
     * 管理员登录
     *
     * @param admin
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("login")
    public String login(HttpServletRequest request, HttpServletResponse response, Admin admin) throws IOException, ServletException {
        Admin admin1 = adminService.selectByAccount(admin.getAccount());
        if (admin1 == null) {//登录失败
            request.setAttribute("message", "用户名或密码错误");
            return "back/login";
        } else if (admin.getPassword().equals(admin1.getPassword())) {// 登录成功
            request.getSession().setAttribute("admin", admin);
            response.sendRedirect(request.getContextPath() + "/back/back.html");
        }
        request.setAttribute("message", "用户名或密码错误");
        return "back/login";
    }

    /**
     * 注销
     * @param request
     * @return
     */
    @GetMapping("loginOut")
    public String loginOut(HttpServletRequest request){
        request.getSession().setAttribute("admin",null);
        return "back/login";
    }
}
